Page({
  data: {
    recive_name:"",
    phone:"",
    address:"",
    ad_id:"",

    choice_txt:"请选择",
    province:[],
    object_province:[],

    arrIndex: 0,
    index: 0,

    city:[],
    object_city:[],
    choice_city_txt:"请选择",
    arrIndex_city: 0,
    index_city: 0,

    area:[],
    object_area:[],
    choice_area_txt:"请选择",
    arrIndex_area: 0,
    index_area: 0,

    address_id_for_edit:"",
    from_page:"",

  },
  onLoad(e) {
    console.log('in address-manage');
     console.log(e);
     this.data.from_page = e.from_page;
     if(e.id!=undefined){//修改
      console.log('in update');
      
         var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-address'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "address_id":e.id,
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          console.log(res.data.data);
          this.setData({
            recive_name:res.data.data.name,
            phone : res.data.data.phone,
            address : res.data.data.address,
            // choice_txt : res.data.data.province,
            // choice_city_txt
            address_id_for_edit:e.id
          })

          
        }
      },
    });
      //  this.setData({
      //    address_id_for_edit:e.id,
      //  })
     }
     this.load_province();
    },
    
    //省份选择
    bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    // console.log(this.data.object_province[e.detail.value]);

    this.setData({
      index: e.detail.value,
      choice_txt:this.data.object_province[e.detail.value].area_name,
      city:[],
    object_city:[],
    choice_city_txt:"请选择",
    arrIndex_city: 0,
    index_city: 0,

    area:[],
    object_area:[],
    choice_area_txt:"请选择",
    arrIndex_area: 0,
    index_area: 0,

    });
     console.log(this.data.object_province[e.detail.value]);
    this.load_province(this.data.object_province[e.detail.value].area_code,2);
  },

  //区选择
  bindPickerChange_area(e){
       console.log('picker发送选择改变，携带值为', e.detail.value);
    // console.log(this.data.object_province[e.detail.value]);
    
    this.setData({
      index_area: e.detail.value,
      choice_area_txt:this.data.object_area[e.detail.value].area_name
    });
     console.log(this.data.object_area[e.detail.value]);
    this.load_province(this.data.object_area[e.detail.value].area_code,3);
  },

  //选中城市
  bindPickerChange_city(e){
       console.log('picker发送选择改变，携带值为', e.detail.value);
    // console.log(this.data.object_province[e.detail.value]);
  
    this.setData({
      index_city: e.detail.value,
      choice_city_txt:this.data.object_city[e.detail.value].area_name,
      area:[],
    object_area:[],
    choice_area_txt:"请选择",
    arrIndex_area: 0,
    index_area: 0,
    });
     console.log(this.data.object_city[e.detail.value]);
    this.load_province(this.data.object_city[e.detail.value].area_code,3);
  },
   
  //读取省
  //level = 1省 ;2读取市；3读取区
  load_province(area,level){
      var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-areas-by-citycode'

      var json = [];
      
      console.log(area);
      if(area != undefined){
         json = {"code":area,"level":level}; 
        }
        else{json = {"level":1};}
        console.log(json);
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "code":json['code'],
            "level":json['level'],
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          console.log(res.data.data[1]);
          
          if(level == undefined || level ==1)
          this.setData({
              province : res.data.data[1],
              object_province : res.data.data[0] 
            })
            
            if(level==2)
               this.setData({
                 city:res.data.data[1],
                 object_city:res.data.data[0]
                })
                
            if(level == 3)
              this.setData({
                area:res.data.data[1],
                object_area:res.data.data[0]
              })
          
          
        }
      },
    });

  },


  inputName(e){
       console.log(e);
       var val = e.detail.value;
       console.log(val);
       this.setData({
         recive_name:val
       })
      },

      inputPhone(e){
       console.log(e);
       var val = e.detail.value;
       console.log(val);
       this.setData({
         phone:val
       })
      },

      inputAddress(e){
        var val = e.detail.value;
        this.setData({
          address:val
        })
      },
      
  //保存
  submit(e){
  
    if(this.data.choice_txt=='请选择' || this.data.choice_area_txt=='请选择' || this.data.choice_city_txt=='请选择')
    {
      my.showToast({
        content: '请选择所在地区', // 文字内容
        success: (res) => {
          
        },
      });
      return;
    }
    console.log(this.data)
    if(this.data.address == '' || this.data.address.length<3){
      my.showToast({
        content: '详细地址填写不正确，请重新填写!', // 文字内容
        success: (res) => {
          
        },
      });
      return;
    }

    if(this.data.recive_name == ''){
       my.showToast({
        content: '请填写收货人!', // 文字内容
        success: (res) => {
          
        },
      });
      return;
    }

    var regMobile = /^1\d{10}$/;
    // regMobile.test(this.data.phone);
     if(!regMobile.test(this.data.phone)){
       my.showToast({
        content: '请填写正确的手机号!', // 文字内容
        success: (res) => {
          
        },
      });
      return;
    }
    
    if(this.data.choice_txt == '' || this.data.choice_city_txt == '' || this.data.choice_area_txt==''){
      my.showToast({
        content: '请选择正确的地区', // 文字内容
        success: (res) => {
          
        },
      });
      return;
    }

    var edit_id = "";
    if(this.data.address_id_for_edit.length>1){
      edit_id = this.data.address_id_for_edit;
    }

    //submit
     var app = getApp()
      var url = app.globadData.apiUrlHeader+'add-or-update-address'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "province":this.data.choice_txt,
            "city":this.data.choice_city_txt,
            "region":this.data.choice_area_txt,
            "address":this.data.address,
            "name":this.data.recive_name,
            "phone":this.data.phone,
            "address_id":edit_id,
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          console.log(res.data);
          if(this.data.from_page == '' || this.data.from_page == undefined || this.data.from_page == 'undefined'){
           
           url = `/pages/main/address/address`;
            console.log(this.data.from_page);
          }
          else
            url = `/pages/main/${this.data.from_page}/${this.data.from_page}?checkedAddressId=${res.data.data}`
          // url = `/pages/main/address/address`;
    my.navigateTo({
      url: url, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
        }
      },
    });

  },

  


});
